//
//  DiscountMoreVC.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "DiscountMoreVC.h"
#import "DataHandler.h"
#import "DiscountMoreTableViewCell.h"
#import "DiscountMoreSmallImageTableViewCell.h"
#import "DiscountMoreTextTableViewCell.h"

#import "DiscountMoreDataModel.h"
#import "DiscountMoreHot_areaListModel.h"
#import "DiscountMoreHot_areaPlaceModel.h"
#import "DiscountMoreHot_areaModel.h"


#import "DiscountMoreDidcount_topicModel.h"
#import "DiscountMoreDidcount_topicTopicModel.h"
#import "DiscountMoreDidcount_topicListModel.h"
#import "DiscountMoreBgImageTableViewCell.h"

#import "DiscountMoreHot_goodsModel.h"
#import "DiscountMoreYHHTableViewCell.h"
#import "DiscountHotVC.h"
#import "DiscountMoreDetailsVC.h"

#import "DiscountMoreSeachDataModel.h"
#import "DiscountMoreSeachListDataModel.h"



#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

//静态变量 标识符
static NSString *const DiscountMoreReuse = @"DiscountMoreReuse";
static NSString *const DiscountMoreSmallReuse = @"DiscountMoreSmallReuse";
static NSString *const DiscountMoreTextReuse = @"DiscountMoreTextReuse";
static NSString *const DiscountMoreBgTextReuse = @"DiscountMoreBgTextReuse";
static NSString *const DiscountMoreYHHTextReuse = @"DiscountMoreYHHTextReuse";


@interface DiscountMoreVC ()

@property(nonatomic, strong) UITableView *myTabelView;

//headerButton
@property (nonatomic, strong) UIButton *JJZYXButton;
@property (nonatomic, strong) UIButton *JPButton;
@property (nonatomic, strong) UIButton *QZButton;
@property (nonatomic, strong) UIButton *WDHKButton;
@property (nonatomic, strong) UIButton *DDYButton;
@property (nonatomic, strong) UIButton *JDMPButton;
@property (nonatomic, strong) UIButton *JTPJButton;
@property (nonatomic, strong) UIButton *BCJSJButton;

@property (nonatomic, strong) DiscountMoreHot_areaModel *hot_areaModel1;
@property (nonatomic, strong) DiscountMoreHot_areaModel *hot_areaModel2;
@property (nonatomic, strong) DiscountMoreHot_areaModel *hot_areaModel3;
@property (nonatomic, assign) NSInteger listCount1;
@property (nonatomic, assign) NSInteger listCount2;
@property (nonatomic, assign) NSInteger listCount3;


@end

@implementation DiscountMoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
     [self getData];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
    view.backgroundColor = [UIColor colorWithRed:53 / 255.0 green:176 /255.0 blue:131 / 255.0 alpha:1.0];
    [self.view addSubview:view];
    
    self.myTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT - 60) style:UITableViewStylePlain];
    self.myTabelView.backgroundColor = [UIColor whiteColor];
    self.myTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTabelView];
    
//    //搜索
//    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
//    self.searchController.searchResultsUpdater = self;
//    //    设置开始搜索时背景显示与否 页面背景阴影效果,一般不要设置.
//     self.searchController.dimsBackgroundDuringPresentation = NO;
//    self.mySearchBar = self.searchController.searchBar;
////    [[self.mySearchBar.subviews objectAtIndex:0] removeFromSuperview];
//    //设置点击搜索框时候隐藏导航栏
//    self.searchController.hidesNavigationBarDuringPresentation = NO;
//    self.searchController.searchBar.frame = CGRectMake(100, 15, WIDTH / 2, 30.0);
////    self.searchController.searchBar.backgroundColor = [UIColor whiteColor];
//    //    [searchBar sizeToFit]:设置searchBar位置自适应
//    [self.searchController.searchBar sizeToFit];

    
    //注册
    [self.myTabelView registerClass:[DiscountMoreSmallImageTableViewCell class] forCellReuseIdentifier:DiscountMoreSmallReuse];
    
    UINib *nib = [UINib nibWithNibName:@"DiscountMoreTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTabelView registerNib:nib forCellReuseIdentifier:DiscountMoreReuse];
    

    [self.myTabelView registerClass:[DiscountMoreTextTableViewCell class] forCellReuseIdentifier:DiscountMoreTextReuse];
    
    UINib *nibBg = [UINib nibWithNibName:@"DiscountMoreBgImageTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTabelView registerNib:nibBg forCellReuseIdentifier:DiscountMoreBgTextReuse];
    
    [self.myTabelView registerClass:[DiscountMoreYHHTableViewCell class] forCellReuseIdentifier:DiscountMoreYHHTextReuse];
    
    //添加头视图
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 280)];
    headerView.backgroundColor = [UIColor whiteColor];
    self.myTabelView.tableHeaderView = headerView;
    
    //成为代理人
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    [self.view addSubview:self.searchController.searchBar];
    
    //addButon
    [self addButtonBounds];
    
    //返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, 30, 20, 20);
//    button.backgroundColor = [UIColor redColor];
    [button setBackgroundImage:[UIImage imageNamed:@"icon-fanhui 64x64"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAtcion) forControlEvents:UIControlEventTouchUpInside];
    
    // Do any additional setup after loading the view.
}

- (void)buttonAtcion{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//data
-(void)getData{
    self.DidcountMoreArrayM = [NSMutableArray array];
    self.hot_areaArrayM = [NSMutableArray array];
    self.hot_goodsArrayM = [NSMutableArray array];
    self.discount_topicArrayM = [NSMutableArray array];
    
    DataHandler *handler1 = [[DataHandler alloc] init];
    [handler1 getDiscountMoreNotes:^(id result) {
        self.DidcountMoreArrayM = [NSMutableArray arrayWithArray:result];
        DiscountMoreDataModel *dataModel = [self.DidcountMoreArrayM objectAtIndex:0];
        self.hot_areaArrayM = dataModel.hot_area;
        self.discount_topicArrayM = dataModel.discount_topic;
        self.hot_goodsArrayM = dataModel.hot_goods;
        [self.myTabelView reloadData];
        
    } failedHandler:^(NSError *error) {
        
    }];
}

//1.add button
-(void)addButtonBounds{
    self.JJZYXButton = [self addIcon:@"机酒自由行" heghtNumber:1 rowNumbel:1];
    self.JPButton = [self addIcon:@"机票" heghtNumber:2 rowNumbel:1];
    self.QZButton = [self addIcon:@"签证" heghtNumber:3 rowNumbel:1];
    self.WDHKButton = [self addIcon:@"WIFI/电话卡" heghtNumber:4 rowNumbel:1];
    self.DDYButton = [self addIcon:@"当地游" heghtNumber:1 rowNumbel:2];
    self.JDMPButton = [self addIcon:@"景点门票" heghtNumber:2 rowNumbel:2];
    self.JTPJButton = [self addIcon:@"交通票劵" heghtNumber:3 rowNumbel:2];
    self.BCJSJButton = [self addIcon:@"包车/接送机" heghtNumber:4 rowNumbel:2];
}
-(UIButton *)addIcon:(NSString *)labelName heghtNumber:(NSInteger )heght rowNumbel:(NSInteger )row{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.backgroundColor = [UIColor blueColor];
    button.frame = CGRectMake(40 + (65 + 25) * (heght - 1), 30 + 125 * (row - 1), 65, 65);
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 32.5;
    [self.myTabelView addSubview:button];
    button.tag = 1000000 + heght;
    [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"Mall_Home_%ld_%ld",heght, row ]] forState:UIControlStateNormal];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(35 + (65 + 25) * (heght - 1), 100 + 125 * (row - 1) ,75, 25)];
    label.text = labelName;
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    [self.myTabelView addSubview:label];
    return button;
}


//分区的个数
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.hot_areaArrayM.count == 0) {
        return 0;
    }
  return 6;
}

//分区头高度
-(CGFloat )tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 50;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //背景颜色
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(55, 0, WIDTH - 100 , 60)];
    [view addSubview:imageV];
    
    //分隔线
    UILabel *FGX = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH , 10)];
    FGX.backgroundColor = [UIColor colorWithRed:232 / 255.0 green:230 / 255.0 blue:233 / 255.0 alpha:1.0];
    [view addSubview:FGX];
    
    //标题
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, WIDTH -30 , 30)];
    text.textAlignment = NSTextAlignmentCenter;
    [view addSubview:text];
    text.textColor = [UIColor blackColor];
    
    //文字定义
    if (section == 0) {
        text.text = @"欧洲美国";
        imageV.image = [UIImage imageNamed:@"discount_oumei"];
        
    }
    if (section == 1) {
        text.text = @"东南亚";
        imageV.image = [UIImage imageNamed:@"discount_riben"];

    }
    if (section == 2) {
        text.text = @"港澳台";
        imageV.image = [UIImage imageNamed:@"discount_gangao"];
    }
    if (section == 3) {
        text.text = @"低价急卖! 机酒跳楼价";
    }
    if (section == 4) {
        text.text = @"寻人启事 双人海盗没你不行";
    }
    if (section == 5) {
        text.text = @"有好货";
    }
    
    return view;
}

//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section <= 2) {
        if (indexPath.row == 0) {
            return 105;
        }
        if (indexPath.row == 3) {
            return 40;
        }
        return 120;

    }
    if (indexPath.section >2 && indexPath.section <= 4) {
        if (indexPath.row == 0) {
            return 140;
        }
        if (indexPath.row == 4) {
            return 40;
        }
        return 120;
    }
    return 225 * 3 + 20 * 2;
}

//cell数
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section <= 2) {
        return 4;
    }
    if (section == 5) {
        return 1;
    }
    return 5;
}

//建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    if (section <= 2) {
        DiscountMoreHot_areaModel *area = [self.hot_areaArrayM objectAtIndex:section];
        if (row == 0) {
            DiscountMoreSmallImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DiscountMoreSmallReuse];
            cell.myPlaceArrayM = area.place;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        if (row == 1) {
            DiscountMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DiscountMoreReuse];
            DiscountMoreHot_areaListModel *listModel = [area.list objectAtIndex:row - 1];
            cell.myListModel = listModel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        if (row == 2) {
            DiscountMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DiscountMoreReuse];
            DiscountMoreHot_areaListModel *listModel = [area.list objectAtIndex:row - 1];
            cell.myListModel = listModel;
            return cell;
        }
            DiscountMoreTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DiscountMoreTextReuse];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        
    }
    if ((section >= 3) && (section <= 4)) {
        DiscountMoreDidcount_topicModel *area2 = [self.discount_topicArrayM objectAtIndex:section - 3];
        if (row == 0) {
            DiscountMoreBgImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DiscountMoreBgTextReuse];
            DiscountMoreDidcount_topicTopicModel *model = area2.topicNO;
            cell.myModel = model;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        if (row >= 1 && row <= 3) {
            DiscountMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DiscountMoreReuse];
            DiscountMoreDidcount_topicListModel *listModel = [area2.list objectAtIndex:row - 1];
            cell.myModel = listModel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
       DiscountMoreTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DiscountMoreTextReuse];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (section == 5) {
        DiscountMoreYHHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DiscountMoreYHHTextReuse];
        cell.myGoodsArrayM = self.hot_goodsArrayM;
        
        __block DiscountMoreVC *vc = self;
        cell.myBlock = ^(NSInteger idHot, NSString *title){
            DiscountMoreDetailsVC *discountHotVC = [[DiscountMoreDetailsVC alloc] init];
            discountHotVC.discountId = [NSString stringWithFormat:@"%ld", idHot];
            
            for (DiscountMoreHot_goodsModel *model in vc.hot_goodsArrayM) {
                if (model.idHot_goods == idHot) {
                     discountHotVC.myDiscountMoreFaveriteIDBlock = ^(BOOL isFavorite, NSString *discountId){
                        if (isFavorite == YES) {
                            //用户名为空的时候点击登陆, 不是空的时候收藏
                            NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
                            NSString *userName = [defa objectForKey:@"userName"];
                            [[DataBaseHandle shareDataBaseHandle]   insertDiscountMore3:model withUserName:userName withSQname:@"discount"];
                        }
                    };
                    
                }
            }
            
            [self presentViewController:discountHotVC animated:YES completion:^{
                
            }];
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
     if (indexPath.section <= 2) {
         DiscountMoreHot_areaModel *area = [self.hot_areaArrayM objectAtIndex:section];
         if ((row == 1) || (row == 2)) {
             DiscountMoreHot_areaListModel *listModel = [area.list objectAtIndex:row - 1];
             DiscountMoreDetailsVC *discountHotVC = [[DiscountMoreDetailsVC alloc] init];
             discountHotVC.discountId = listModel.idHot_area;
             [self presentViewController:discountHotVC animated:YES completion:^{
                 
             }];
             discountHotVC.myDiscountMoreFaveriteIDBlock = ^(BOOL isFavorite, NSString *discountId){
                 if (isFavorite == YES) {
                     //用户名为空的时候点击登陆, 不是空的时候收藏
                     NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
                     NSString *userName = [defa objectForKey:@"userName"];
                     DiscountMoreHot_areaListModel *listModel = [area.list objectAtIndex:row - 1];
                     [[DataBaseHandle shareDataBaseHandle] insertDiscountMore:listModel withUserName:userName withSQname:@"discount"];
                 }
             };
         }
     }
    
    if ((section >= 3) && (section <= 4)){
        DiscountMoreDidcount_topicModel *area2 = [self.discount_topicArrayM objectAtIndex:section - 3];
        if ((row == 1) || (row == 2) || (row == 3)) {
            DiscountMoreDidcount_topicListModel *listModel = [area2.list objectAtIndex:row - 1];
            DiscountMoreDetailsVC *discountHotVC = [[DiscountMoreDetailsVC alloc] init];
            discountHotVC.discountId = listModel.idlist;
            [self presentViewController:discountHotVC animated:YES completion:^{
                
            }];
            
            discountHotVC.myDiscountMoreFaveriteIDBlock = ^(BOOL isFavorite, NSString *discountId){
                if (isFavorite == YES) {
                    //用户名为空的时候点击登陆, 不是空的时候收藏
                    NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
                    NSString *userName = [defa objectForKey:@"userName"];
                    DiscountMoreDidcount_topicListModel *listModel2 = [area2.list objectAtIndex:row - 1];
                    [[DataBaseHandle shareDataBaseHandle]  insertDiscountMore2:listModel2 withUserName:userName withSQname:@"discount"];
                }
            };
            
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
